﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OF.Component.Base;
using OF.Component.BLL;
using OF.Component.Model;
using OF.Utility;
using System.IO;

namespace _2010web.Manage.webinfo
{
    public partial class AuthorityUserAdd : OpBasePage
    {
        protected int intID = 0;
        protected int Language = 0;
        protected int DealerID = 0;
        protected string Message = "";
        protected string strsytle = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            SetPageCode(101, 10101);
            if (!Page.IsPostBack)
            {
                DealerID = RequestUtility.GetQueryInt("id", -1);
                OpUserInforInfo info = OpUserInforOperate.GetByID(DealerID);
                if (info != null)
                {
                    this.txtName.Text = info.OpUserName;
                    this.txtEjLoginName.Text = info.LoginName;
                    this.txtEjLoginName.ReadOnly = true;
                    this.txtOldPass.Text = SecurityUtility.UnEncryptAsc(info.PassWord);
                    txtMobile.Text = info.ContactMobile;
                    RoleBind(info.RoleId);
                }
                else
                {
                    strsytle = "style='display:none;'";
                    this.txtOldPass.Text = "新增的可以忽略";
                    RoleBind(0);
                }
            }
        }

        private void RoleBind(int roleId)
        {
            int num = 0;
            IList<AuthorityRoleInfo> list = AuthorityRoleOperate.Search(out num, 1, 1000, "SystemID=1 and isactive=1", "RoleID", false);
            ListItem item = new ListItem();
            item.Text = "请选择角色";
            item.Value = "";
            this.RoleList.Items.Add(item);
            var ParentList = from AuthorityRole in list
                             where AuthorityRole.ParentID == 0
                             select new { RoleName = AuthorityRole.RoleName, RoleID = AuthorityRole.RoleID };
            foreach (var li in ParentList)
            {
                item = new ListItem();
                item.Text = li.RoleName;
                item.Value = li.RoleID.ToString();
                if (li.RoleID == roleId)
                {
                    item.Selected = true;
                }
                this.RoleList.Items.Add(item);

                var ChildList = from AuthorityRole in list
                                where AuthorityRole.ParentID == li.RoleID
                                select new { RoleName = AuthorityRole.RoleName, RoleID = AuthorityRole.RoleID };

                foreach (var cv in ChildList)
                {
                    item = new ListItem();
                    item.Text = string.Format("    |----{0}", cv.RoleName);
                    item.Value = cv.RoleID.ToString();
                    if (cv.RoleID == roleId)
                    {
                        item.Selected = true;
                    }
                    this.RoleList.Items.Add(item);
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {

            DealerID = RequestUtility.GetQueryInt("id", -1);
            string strMessage = "修改成功！";
            string strUrl = "authorityuserlist.aspx?id=" + DealerID;
            if (DealerID > -1)
            {
                OpUserInforInfo info = OpUserInforOperate.GetByID(DealerID);
                if (info != null)
                {
                    info.ContactMobile = txtMobile.Text.Trim();

                    if (info.PassWord == SecurityUtility.EncryptAsc(this.txtOldPass.Text))
                    {
                        if (!string.IsNullOrEmpty(txtNewPass.Text))
                        {
                            info.PassWord = SecurityUtility.EncryptAsc(this.txtNewPass.Text);
                        }
                        info.RoleId = int.Parse(this.RoleList.SelectedValue);
                        info.CreateDateTime = DateTime.Now;
                        OpUserInforOperate.Update(info);
                        Message = " $().AjaxManage.MessageOpen(\"" + strMessage + "\");;setTimeout(\"window.location.href='" + strUrl + "'\", 1000);";

                    }
                    else
                    {
                        strMessage = "原密码不正确请重新输入！";
                        //Message = " $().AjaxManage.MessageOpen(\"" + strMessage + "\");;setTimeout(\"window.location.href='" + strUrl + "'\", 1000);";
                        Message = MessageDivInfoNoRedirect(strMessage);

                    }
                }
               
            }
            else
            {
                OpUserInforInfo info = OpUserInforOperate.GetByLoginName(this.txtEjLoginName.Text);
                if (info == null)
                {
                    info = new OpUserInforInfo();
                    info.LoginName = this.txtEjLoginName.Text;
                    info.ContactMobile = txtMobile.Text.Trim();
                    info.PassWord = SecurityUtility.EncryptAsc(this.txtNewPass.Text);
                    info.LastLoginDate = DateTime.Now;
                    info.LoginCount = 0;
                    info.IsActive = 1;
                    info.OpUserName = this.txtName.Text;
                    info.CreateDateTime = DateTime.Now;
                    info.UpdateTime = DateTime.Now;
                    info.RoleId = int.Parse(this.RoleList.SelectedValue);
                    OpUserInforOperate.Insert(info);
                    strMessage = "账户添加成功！";
                    Message = " $().AjaxManage.MessageOpen(\"" + strMessage + "\");;setTimeout(\"window.location.href='authorityuserlist.aspx'\", 1000);";

                }
                else
                {
                    strMessage = "登陆名已经存在，请重新输入！";
                    Message = " $().AjaxManage.MessageOpen(\"" + strMessage + "\");;setTimeout(\"window.location.href='" + strUrl + "'\", 1000);";
                }

            }

        }
    }
}